from marshmallow import Schema, fields, validate, validates, ValidationError
from datetime import datetime

class BillingItemSchema(Schema):
    """账单项目模式"""
    type = fields.Str(required=True, validate=validate.OneOf(['registration', 'prescription', 'medical_record', 'service', 'other']))
    itemId = fields.Str()
    description = fields.Str(required=True)
    amount = fields.Float(required=True)

class InsuranceInfoSchema(Schema):
    """医保信息模式"""
    isInsurance = fields.Bool(dump_default=False)
    insuranceNumber = fields.Str()
    reimbursementRatio = fields.Float()
    patientPay = fields.Float()
    insurancePay = fields.Float()

class BillingSchema(Schema):
    """账单模式"""
    billingId = fields.Str()
    patientId = fields.Str(required=True)
    visitId = fields.Str()
    items = fields.List(fields.Nested(BillingItemSchema), required=True)
    totalAmount = fields.Float(required=True)
    insuranceInfo = fields.Nested(InsuranceInfoSchema)
    status = fields.Str(validate=validate.OneOf(['待支付', '已支付', '已取消', '部分支付', '已退款']), dump_default='待支付')
    createdAt = fields.DateTime(dump_default=datetime.utcnow)
    updatedAt = fields.DateTime(dump_default=datetime.utcnow)

class BillingUpdateSchema(Schema):
    """账单更新模式"""
    items = fields.List(fields.Nested(BillingItemSchema))
    totalAmount = fields.Float()
    insuranceInfo = fields.Nested(InsuranceInfoSchema)
    status = fields.Str(validate=validate.OneOf(['待支付', '已支付', '已取消', '部分支付', '已退款']))
    
class BillingQuerySchema(Schema):
    """账单查询模式"""
    patientId = fields.Str()
    visitId = fields.Str()
    status = fields.Str(validate=validate.OneOf(['待支付', '已支付', '已取消', '部分支付', '已退款', 'all']))
    startDate = fields.Date()
    endDate = fields.Date() 